local repstorage = game.ReplicatedStorage local event = repstorage:WaitForChild("StartTimer") script.Parent.Touched:Connect(function(hit) local plr if hit.Parent:IsA("Model") then plr = hit.Parent end if hit.Parent.Parent:IsA("Model") then plr = hit.Parent.Parent end if plr then else return end if game.Players:FindFirstChild(plr.Name) then plr = game.Players:FindFirstChild(plr.Name) else return end event:FireClient(plr) print("fired") end)
What did i do wrong, also it doesn't even print "fired" or fire the client
You have WAY too many if statements necessary for your FireClient()
. Here's how you can do it:
local repstorage = game.ReplicatedStorage local event = repstorage.StartTimer script.Parent.Touched:Connect(function(hit) local plr = game.Players:GetPlayerFromCharacter(hit.Parent) if plr then event:FireClient(plr) end end)
GetPlayerFromCharacter()
immediately gets the player, and so you can use that instead of just doing it plr = hit.Parent
.
Step-By-Step explanation
(This is just to add a bit more of a description
as to why it didn't actually fire.)
:FireClient
to an instance that is null, THERE'S no plr parameter on your script, which again leads to another error.Make sure you've defined the player, also try to avoid using many if statement's in your code that are not needed, as it makes it harder to debug.
Use GetPlayerFromCharacter()
as DovyDas stated, because it gets the player who has made a contact with a 3D dimensional object, and is generally easier than using if-statements as the function returns a player hence the name "GetPlayerFromCharacter"
(You can use anyone of these scripts stated, as they both act and do the same thing.)
1st script:
local repstorage = game.ReplicatedStorage local event = repstorage:WaitForChild("StartTimer") local player = game:GetService("Players").PlayerAdded:Connect(function(plr) local Part = game.Workspace.Part Part.Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then event:FireClient(plr) print("fired") end end) end)
2nd script:
local player = game:GetService("Players").PlayerAdded:Connect(function(p) local repstorage = game.ReplicatedStorage local event = repstorage:WaitForChild("RemoteEvent") -- change to your remoteevent name local part = game.Workspace.Part part.Touched:Connect(function(hit) local plr = nil if hit.Parent:IsA("Model") then plr = hit.Parent end if hit.Parent.Parent:IsA("Model") then plr = hit.Parent.Parent end if plr then else return end if game.Players:FindFirstChild(plr.Name) then print (7) plr = game.Players:FindFirstChild(plr.Name) end event:FireClient(p) print("fired") end) end)
P.S: This was kind of rushed, so sorry for bad grammar!
If you need any further help, don't hesitate to comment, or leave me a DM!