I’m trying to make a merchant screen pop up when a part is touched. It doesn’t work because the player is nil. I used a RemoteEvent to make it work. Help is appreciated and here are my scripts:
ServerScript(Working):
local d = false -- d is debounce
game.Workspace.Merchant.Circle.Touched:Connect(function(hit)
if hit then
if d == false then
d = true
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if not plr then return end
game.ReplicatedStorage.Merchant.OpenMerchant:FireClient(plr)
wait(3)
d = false
end
end
end)
CilentScript(Prints nil)
game.ReplicatedStorage.Merchant.OpenMerchant.OnClientEvent:Connect(function(plr)
print(plr) -- Instead of printing the plr, it prints "nil"
if plr then
plr.PlayerGui.Game.Merchant.Visible = true
end
end)
Hey! I noticed the issue in your code. In the client script, you don’t need the plr argument. Instead, you can directly use game.Players.LocalPlayer to reference the player. When the client event is triggered, just toggle the UI as needed. However, if you prefer to pass the plr argument again, you can modify the server script to send it when firing the event. To do this, simply change the game.ReplicatedStorage.Merchant.OpenMerchant:FireClient(plr) line to game.ReplicatedStorage.Merchant.OpenMerchant:FireClient(plr, plr).
When I touch the part and it pops up, I wait a little bit more than 3 seconds and then close it. After that, it never popped up again.
Sometimes I also spawn to go there and then I touch the part and it didn’t pop up.
Do you know whats wrong?
EDIT: I set d to false again if there wasn’t a player. It is solved now