#event problem

253 messages · Page 1 of 1 (latest)

vernal hearth
#

here is a server script :
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("2")

for _, player in pairs(game.Players:GetPlayers()) do
event:FireClient(player, player.Name)
end
a local script inside a tool :
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("2")

event.OnClientEvent:Connect(function(playerName)
print("Received player name:", playerName)
end)
so im trying to make the server event check the players then send it to the local script by an event so the problem that its not printing it .

exotic ibex
strange zenith
#

The player is not passed through from server to client

exotic ibex
#

ohhh

fathom cypressBOT
#

studio** You are now Level 9! **studio

exotic ibex
#

wait so hes tryna fire from server to client

strange zenith
#

The player is only acessed from the first paramater from client to server

vernal hearth
strange zenith
#

I don’t really see a problem with this script also it is kind of hard to read can you put it in box format?

exotic ibex
#

three of these: `

strange zenith
#

I can’t do it right now because I am on phone

vernal hearth
#

ok

strange zenith
#

Yea that

#

Then type lua

#

Then paste the code then end it with three more

vernal hearth
#

using ur advice the scripts will be :
local script :

local event = ReplicatedStorage:WaitForChild("2")

event.OnClientEvent:Connect(function(plr, name)
    print(plr, name)
end)

server script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("2")

for _, player in pairs(game.Players:GetPlayers()) do
    event:FireClient(player, player.Name)
end


strange zenith
#

Alr

exotic ibex
#

the local script* should be OnServerEvent if im correct

vernal hearth
#

i cannot

#

there is no onserverevent

#

its js OnClientEvent

strange zenith
#

I mean I still don’t see a problem maybe change the variable name 2 to like Event or something?

vernal hearth
#

ok

strange zenith
#

Also are there any errors

vernal hearth
#

wait lemme change the name and check

#

there is no errors + its printing nothing in the output

#

by the way

#

the local script is inside a tool in starterpack

#

and the event in server storage and the serverscript in ServerScriptService

strange zenith
#

Hm

exotic ibex
#

well

#

It doesnt even look like the loop is running

strange zenith
#

Test the loop

exotic ibex
#

wait

#

i got it

strange zenith
#

Put a print statment

vernal hearth
exotic ibex
#

maybe i dont

vernal hearth
#

just say it

exotic ibex
#

Okay so the loop isnt running as no prints come out of the loop

strange zenith
#

Does get players return a table I have never used that before

vernal hearth
#

yea it should print from the local script

#

u know what

strange zenith
#

ok

#

so the issue is that

#

the get players is probably returning a empty table

vernal hearth
#

that wasnt even the main thing i wanted to do i maked it print so i can check if its even working then it didnt then i come here, the thing i wanted to do is the script check the players in the game then chekc if they have a tool called Stick if so then it will print them

strange zenith
#

here lemme re write the code

vernal hearth
#

k

strange zenith
#

try this replace the server script

exotic ibex
#

yea its an empty list

strange zenith
#

oh wait

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("2")

game.Players.PlayerAdded:Connect(function(plr)
        event:FireClient(plr, plr.Name)

end)
#

new code @vernal hearth

vernal hearth
#

ok lemme check

strange zenith
#

just completly replace that server script with this

vernal hearth
#

i changed in it and added a print after the PlayerAdded function and it didnt even print the plr

#

thats weird

#

wait nvm

#

nvm mt bad

#

it worked

strange zenith
#

alr perfect

vernal hearth
#

in the server script it printed my name and in the locl it printed my name , nil

exotic ibex
#

Your original code didnt work cause it was going through an empty list, I did a task.wait(5) and it had a non empty list

vernal hearth
#

ohhhh ok

exotic ibex
#

it ran before the player could be loaded or smthin

strange zenith
#

yea but that is a bad way to get the players because you're only firing the event to the players that have joined 5 seconds before

vernal hearth
#

now i wanna check if that player had a tool called stick should i use plr.Backpack or plr:WaitForChild("Stick")

strange zenith
#

remove the second variable in the local script called name and rename the plr variable name

vernal hearth
#

i did

exotic ibex
strange zenith
#

well

#

plr.Backpack:WaitForChild("Stick")

#

or no

#

you would findfirstchild and see if it returns true

vernal hearth
fathom cypressBOT
#

studio** You are now Level 5! **studio

exotic ibex
strange zenith
#

because if the player does not have the item you would be stuck in a infinite waiting loop

vernal hearth
#

ty

exotic ibex
#

returns true and passes into the conditional

strange zenith
#

also the playeradded would be the best in this situation because the getplayers() would need to updated continuously and could cause lag

vernal hearth
#

oh ok i will remember that

strange zenith
#

that is a infinite while loop right there always checking for a player added to the player list

vernal hearth
#

im new by the way so yea

strange zenith
#

when the player added would be activated by a new player joined

exotic ibex
#

PlayerAdded -- if player joins
PlayerRemoving -- if player is leaving

strange zenith
#

ok so has everything been sorted out?

vernal hearth
#

i think i defind the Stick as tool in the local script by making itlocal tool = script.Parent and use if plr.Backpack:FindFirstChild(tool) then so the hcakers cant js change their thing in backpack to Stick

#

is that better

#

?

short sparrow
#

Don’t worry about security if you’re still new to scripting

#

Focus on making it work and find optimal ways of doing things

vernal hearth
#

ok sure

strange zenith
#

well it's always good to get into a good habitat

vernal hearth
#

this mapp is js for testing so ok ill take ur advice

short sparrow
#

Afterwards we can tell you what most decent “hackers” can retrieve

strange zenith
#

you don't want to be feeding information from client to server that is how hackers will be able to mess your game up

#

never trust the client

short sparrow
#

You can make your own anitcheat quite easily

#

Just add in checks to see if the player is doing things he should not be able to do. E.g. triggering a button while not being near it

strange zenith
#

also make sure if a player buys something you make sure to check the price on the server side

vernal hearth
#

i think that i will do that but it will not ban him right away my idea is to make the players be able to kick a player if they vote for him or the player will be banned if his reports reaches a certian number

#

is that good ?

strange zenith
#

no I would ban any hackers

vernal hearth
#

i mean it could be a glitch or a bug from my game

#

so thats why im not banning right away

strange zenith
#

I mean glitches are not that common like changing values

vernal hearth
#

what about like flying or something common

strange zenith
#

then you can just kick

vernal hearth
#

my game will go down imagaine getting banned for a bug from the game

vernal hearth
strange zenith
#

I am just saying bugs like changing a certain value is not a bug

vernal hearth
#

yea yea i know

strange zenith
#

so you just have to filter them out

vernal hearth
#

that would be a ban

#

ya

#

thank you for helping me

strange zenith
#

np

vernal hearth
#

i was trying to check if the player has the tool Stick

#

and it showed this bug :
Players.kay_714l.Backpack.Stick.LocalScript:5: attempt to index nil with 'Backpack'

short sparrow
#

@strange zenith a ban system would be quite difficult for a new scripter that doesn’t (or barely) know events yet. So I would just keep it at a kick

strange zenith
#

are you acessing that backpack correctly?

vernal hearth
#

yea

strange zenith
#

show the script

#

yea

vernal hearth
#

k

short sparrow
#

You’re trying to get the backpack of nil

#

That’s what that error means

vernal hearth
#

local script inside the tool :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event")
local tool = script.Parent
event.OnClientEvent:Connect(function(plr, name)
    print(plr)
end)

a server script :

local event = ReplicatedStorage:WaitForChild("Event")

game.Players.PlayerAdded:Connect(function(plr)
    
    event:FireClient(plr)

end)```
strange zenith
#

where is the line where you try to acess the backpack

vernal hearth
#

in the local script

strange zenith
#

well include that line

#

also you don't need plr passed as a argument

#

for the local script

#

just change plr to name in the local script then remove name because name will return nil

short sparrow
strange zenith
#

no it does not

short sparrow
#

Yes it does

#

OnClientEvents’ first parameter is the client

strange zenith
#

no it is not?

short sparrow
#

It always returns the client who fires it

strange zenith
#

that is only for server

short sparrow
#

Go and test it

strange zenith
#

alright

short sparrow
#

Why are you using OnClientEvent inside a client script?

vernal hearth
strange zenith
#

that is the way it is supposed to be

vernal hearth
#

yea and there is no OnServerEvent

short sparrow
#

Wait I got them mixed up again

#

I always forget which one goes where. But Server events don’t return the plr automatically

#

So why add it. It’s just players.localplayer

strange zenith
#
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")

game.Players.PlayerAdded:Connect(function(plr)
    Event:FireClient(plr, "Hello")
end)
#
local Event = game.ReplicatedStorage:WaitForChild("RemoteEvent")


Event.OnClientEvent:Connect(function(var1, var2)
    print(var1, var1)
end)
vernal hearth
#

wait imma correct the local script

#

its actully like this :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event")
local tool = script.Parent
event.OnClientEvent:Connect(function(plr, name)
    if plr.Backpack:WaitForChild(tool) then
        print(plr)    
    end
    
end)


#

its not printing plr

#

and

#

its showing error

#

saying

#

Players.kay_714l.Backpack.Stick.LocalScript:5: attempt to index nil with 'Backpack'

strange zenith
#

because plr is the name

#

type at the top local player = ga

#

game.players.localplayer

#

dont copy this line because capitalization is not right

#

then switch out the if plr.backpack to player.backpack

#

then should fix it

vernal hearth
#

k

#

so the final scripts should be
local script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event")
local tool = script.Parent
local player = game.Players.LocalPlayer

event.OnClientEvent:Connect(function(plr, name)
    if plr then
        if player.Backpack:WaitForChild(tool) then
            print(plr)    
        end
    end
    
end)


server script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local event = ReplicatedStorage:WaitForChild("Event")

game.Players.PlayerAdded:Connect(function(plr)
    
    event:FireClient(plr)

end)
strange zenith
#

yes

#

does it wrok?

#

*work

vernal hearth
#

it didnt print any error and it didnt print plr

strange zenith
#

which means you don't have a stick

vernal hearth
#

i do

#

its in my backpack

#

i mean starter pack

strange zenith
#

show me a picture of your backpack

vernal hearth
#

k

strange zenith
#

no no your backpack

#

that is starterpack

vernal hearth
#

yae

#

its in starter back wich means when i join it will automatically be inn my backpack

strange zenith
#

I dont think so

#

like iff you are holding

vernal hearth
#

but when player added

#

it wil lbe on his back pack

strange zenith
#

no

#

you changed it again

vernal hearth
#

thats when the script fire the event and send it to the local script and its checks if it has it and print hte player

strange zenith
#

it is supposed to be findfirstchild not waitforchild

#

unless that is what you want it to be like

vernal hearth
#

oh ok ill change it

#

didnt work

#

u know what ill fix that dw

strange zenith
#

You can just invite me to the studio I will fix it

vernal hearth
#

how

strange zenith
#

Team create

#

Look a video up on yt and add james_colt1

vernal hearth
#

k

#

add me

fathom cypressBOT
#

studio** You are now Level 6! **studio

strange zenith
#

name?

vernal hearth
#

i send u friend req

strange zenith
#

you create your account yesterday _:

vernal hearth
#

yea my main got deleted

strange zenith
#

oh

vernal hearth
#

is there any problem with that tho ?

strange zenith
#

no I just thought it was weird

vernal hearth
#

i added u now

#

try to join

strange zenith
#

k

vernal hearth
#

wlc

strange zenith
#

where is that script at?

#

for the local

vernal hearth
#

ok ill open it foru

strange zenith
#

I got it

vernal hearth
#

ok check it

strange zenith
#

can you click off the script

#

you are modifying

#

it

vernal hearth
#

k

#

imma join look at the error

strange zenith
#

k

vernal hearth
#

are u modifying it ? its not showing the error

#

why i cant see the local script did u delete it ?

strange zenith
#

ok I fixed it

vernal hearth
#

ok lemme see

strange zenith
#

the script function is kind of weird tbh but test it out

vernal hearth
#

it worked

strange zenith
#

what is the point of this script because the player will always have the stick?

vernal hearth
#

its a tag game so when the round start it will give a random player the stick the player who has the stick will die he can give it to anoother player tho

strange zenith
#

alright well as long as the game works

fathom cypressBOT
#

studio** You are now Level 10! **studio

strange zenith
#

or script

#

ok mark this as solved glad I could hlp

vernal hearth
#

ok

#

thank you bro

#

by the way

strange zenith
#

Got you

vernal hearth
#

what was the problem

strange zenith
#

So when the player was loaded from the game.player handler would fire before the character was loaded in so I added a character added handler then fired the event

#

This would be more consistent than just adding a wait

vernal hearth
#

ohh ok

#

it was so simple and it looked difficult

#

lol

strange zenith
#

It wasn’t I was just confused about the point of the script