Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I need help (about the remote event again)! Why doesn't this script work?

script (in the ServerScriptService):

local event = game.ReplicatedStorage:WaitForChild("Event")
event.OnServerEvent:Connect(function(plr,first)
	print(first,plr)
end)

Local script (in the starter Gui):

local event = game.ReplicatedStorage:WaitForChild("Event")
local hello = "HELLO"
game.Players.PlayerAdded:Connect(function(plr)

	event:FireServer(hello)
end)

4 comments

0

I’m very sure everyone has an idea what your issue is

0

in the title: not work? :D

0

ahem , playeradded, ahem in local script

0

can ya fix it (still gave both playeradded and fireserver) ?

3 answers

✓ Accepted answer

I am not sure why you would use a remote event for this, seeing that the PlayerAdded event never fires for the local player on the client and that using a remote for this purpose would be useless and insecure.

Better way to add leaderstats


If you are simply adding leaderstats for a player, the best bet would be to just do it in a server script, it is more secure and more efficient.

you can simply do the following on a server script for leaderstats

game.Players.PlayerAdded:Connect(function(plr)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"

	local stat = Instance.new("StringValue")
	stat.Name = "stat"
	stat.Value = "A stat"
	stat.Parent = ls
	...
	ls.Parent = plr
end)

However, if you are playing to do something else you didn't specify, you can essentially simulate a player added event by running code directly at the top of the local script, as local scripts are cloned into the player / character when they join.

Ex:

-localscript
local event = game.ReplicatedStorage.Event
local plr = game.Players.LocalPlayer

event:FireServer(plr.Name)

1 comment

0

Thanks man.

If you wanted to run something that prints when a player joins, don't use the playeradded function on the client. Remote events are used for when a player does something, such as clicking a button on their GUI, then sending information to the server to run their code. You can just run this in the ServerScriptService and it's the same exact thing but less of a pain:

game.Players.PlayerAdded:Connect(function(plr)
	print("HELLO " .. plr)
end)

There's no need for running a remote event for something the server can detect. To reiterate, just use remote events when the player does something that the server can't detect, such as clicking a GUI button, and then use the remote event to tell the server to run code when the local player does something.

2 comments

0

printing is just an example. I am doing a leaderstats and i want to insert a script inside the stats (which use the remote). So...

0

Why are you using a remote event for that anyways

You don't need to use remote event there.. Just put this script at ServerScriptService And also, you can't print plr, you can only print plr.Name

game.Players.PlayerAdded:Connect(function(plr)
	local hello = "HELLO"
	print(hello.." "..plr.Name)
end)

2 comments

0

you can print an object value using the print function

0

Eh, you can't?! Trying to do so will only prints this to the output: attempt to concatenate local 'plr' (a userdata value)

This discussion is preserved as it was. New answers, comments, and votes are closed.