I have a basic inventory system which works fine, but I want it that when the player clicks the item, it equips it. My only problem is that when I fire the remote event, it doesn't work. As the value, which is script.parent.text goes to the player?
Local script:
script.Parent.MouseButton1Click:Connect(function() local player = game.Players.LocalPlayer game.ReplicatedStorage.KnifeChange:FireServer(player, script.Parent.Text) end)
Server Script:
local function knifeChange(player,value) game.ReplicatedStorage.CurrentKnife.Value = value end game.ReplicatedStorage.KnifeChange.OnServerEvent:Connect(knifeChange)
The player on :FireServer() is already sent as a parameter, so all you have to do is send the extra arguments.
LocalScript, and for the .OnServerEvent() the first parameter passed through is the player, so it will be...
RemoteEvent.OnServerEvent:Connect(function(player, extraArguments) print(player.Name) end)
So, in the LocalScript you don't have to pass through the player, but you will have to pass through the other arguments. In the ServerScript you will have to identify the player as the first parameter passed through.
So, this is how your script should look like.
LocalScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("KnifeChange") script.Parent.MouseButton1Click:Connect(function() local knifeName = script.Parent remote:FireServer(knifeName.Text) end)
ServerScript
local remote = game:GetService("ReplicatedStorage"):WaitForChild("KnifeChange") local currentKnife = game:GetService("ReplicatedStorage"):WaitForChild("CurrentKnife") remote.OnServerEvent:Connect(function(player,knife) currentKnife.Value = knife end)
Common Mistakes
Make sure you are using a StringValue to change the name
Make sure you are using the correct directories, names, etc.
Hope this helped! Feel free to select this as an answer if this worked for you!
The player is already automatically sent as a parameter.
Try this in the local script:
script.Parent.MouseButton1Click:Connect(function() game.ReplicatedStorage.KnifeChange:FireServer(script.Parent.Text) end)