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

How do I get to the player from a click in FE?

I have a part in workspace that should get to a script in ServerScriptService. The part has a ClickDetector and a Script saying:

script.Parent.ClickDetector.MouseClick:connect(function(Player)
    game.ReplicatedStorage:WaitForChild("BindableFunction"):InvokeServer(script.Parent) --Here
print('Click sent')
end)

The script in ServerScriptService says:

local Listener = game.ReplicatedStorage.BindableFunction 
function Listener.OnInvoke(Player, Brick)
    print('Click Received')
    wait(0.3)
    local t = Player.leaderstats.Tokens --Here

However, I get the output of 'leaderstats' is not a valid member of Part in the marked places. Both places have the same output.

How would I make the script get to the player instead of the part? And before I get any comments like 'just switch them around'. That doesn't work.

1 comment

0

Thanks for marking mine as your answer, I'm glad I could help!

2 answers

✓ Accepted answer

You do not need a BindableFunction for this, it just makes it more complicated.

The solution is just have a script in SSS(ServerScriptService) and have this in it:

local part = game:GetService("Workspace"):WaitForChild("YOUR PART HERE")
local clickDetector = part:WaitForChild("ClickDetector")
local players = game:GetService("Players")

clickDetector.MouseClick:Connect(function(plr)
         plr.leaderstats.Tokens --and do your stuff here. If it doesn't work, it probably returns the character model instead of the player object, so just do:
         local player = players:GetPlayerFromCharacter(plr)
         player.leaderstats.Tokens --code here
end)

You don't need a BindableFunction. It is unnecessary because you can do it without it with much better efficiency.

Wow, you over complicated this. To start, you don't need that bindable function, you just do this:

workspace.MyPart.ClickDetector.MouseClick:Connect(function(PlayerThatClicked)
	print(PlayerThatClicked.Name) -- SebbyTheGODKid
	-- Code here
end)

Second thing, InvokeServer() only works on Remote Functions, and you used it on a bindable.

Bindable Function:

local MyBindable = Instance.new("BindableFunction")
MyBindable.OnInvoke = function(argument)
	print(argument)
end)
MyBindable:Invoke("this will be printed") -- > "this will be printed"

vs. Remote Function:

local MyRemote = Instance.new("BindableFunction")
MyRemote.OnServerInvoke = function(argument)
	print(argument)
end)
-- Called from the client:
MyRemote:InvokeServer("this will be printed") -- > "this will be printed"

I hope this clears it up.

Also, for the part about the tokens, if it's a value then you need to do workspace.MyValue.Value NOT workspace.MyValue because "MyValue" is not a valid member of workspace.

Extra resources:

1 comment

0

He should have the script in SSS, not Workspace. SSS is a lot safer. (see my answer below)

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