Ad
Log in to vote
0

Touch remote event shows an error in game but not studio?

Asked by
RjsMc 36
5 months ago

Ok I want to make a platform where you sell your items for money. However while making the touch thingy, it works in studio. However in the game, when I walk on it, it shows an error. ireClient player argument must be a player object - Here are the 2 scripts. Does anyone know any problems?

SERVER SCRIPT

script.Parent.Touched:connect(function(part)
    local plr = part.Parent
    game.ReplicatedStorage.Events.SellGrass:FireClient(plr)
end)

LOCALSCRIPT

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")

game.ReplicatedStorage.Events.SellGrass.OnClientEvent:connect(function(plr) -- Change if event is in different area
    leaderstats.Money.Value = leaderstats.Money.Value + leaderstats.Grass.Value
    leaderstats.Grass.Value = 0
end)


1 answer

Log in to vote
1
Answered by
theCJarmy7 1258 Moderation Voter
5 months ago
Edited 5 months ago

There are a few things wrong with your remote event system here, first of which being that you don't need a remote event. There is no reason you should use a localScript to change the player's money/grass values, just do that on the server. As for the error you receive, that is because part.Parent is the player's character, not the player object itself, a quick fix for this is the following.

local char = part.Parent
local player = game.Players:GetPlayerFromCharacter(char)
 if player then
    remote:FireClient(player)
end

The if player is needed because char is not guaranteed to be a character's model, it could be any random part.

But like I said, you don't need to use a remote event in this situation.

Also, this shouldn't have worked in studio.

0
Well it did lol. Anyways I found out that plr.Parent just gets its character, so I learned the GetPlayerFromCharacter. It ends up working. RjsMc 36 — 5mo
Ad

Answer this question