I have a script that when you touch a part, its supposed to destroy a UI. I have a RemoteEvent, and all my code seems to work fine, but it still doesn’t work. I’ve tried look at the developer forums, and I tried code someone else had wrote and modified it a little to fit my needs, but it still gave me the same error.
Code:
Server, (Located inside of a part thats in workspace)
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local part = script.Parent
part.Touched:Connect(function(Player)
re:FireClient(Player)
end)
Local, (In a textbutton within a ScreenGui that is inside of StarterGui)
Because your specifying the players character, not the actual player.
game.Players:GetPlayerFromCharacter(player.Parent)
--so,
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local part = script.Parent
part.Touched:Connect(function(Player)
re:FireClient(game.Players:GetPlayerFromCharacter(Player.Parent))
end)
What you defined by “Player” (in the Touched function) is not the player object. It’s what touched the part (so one of limbs of the player’s character)
As @regexman did, you can simply use Player.Parent to find the character of the player and use GetPlayerFromCharacter() to find the player.
Here is what I’d do:
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local part = script.Parent
part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
re:FireClient(player)
end
end)
It works good, it triggers when touched and destroys the Gui, like I want it to! I still get the same error though. Perhaps its something wrong with Roblox?
I need help with this script (i’ve edited it so that it only works if leaderstats is above a certain value
Code: (Runs on the server)
local rs = game:GetService("ReplicatedStorage")
local re = rs:WaitForChild("RemoteEvent")
local part = script.Parent
part.Touched:Connect(function(hit)
local character = hit.Parent
local player = game.Players:GetPlayerFromCharacter(character)
if player.leaderstats.Splits.Value < 15 then
if hit.Parent:FindFirstChild("Humanoid") then
re:FireClient(player)
end
end
end)
--player.leaderstats.Splits.Value < 15
--hit.Parent:FindFirstChild("Humanoid")