Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't I fire a client?

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

2 comments

0

Is this a regular script or a localscript?

0

server script

2 answers

✓ Accepted answer

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.

3 comments

0

Use it because it works for me everytime.

0

thats how i learnt to do it from someone

0

@Dovydas1118, please format your answers properly. See my answer as an example, also, you haven't stated why it doesn't work, instead you changed the script.

##Answer:

Step-By-Step explanation

(This is just to add a bit more of a description as to why it didn't actually fire.) ##Problem:

  • The reason it didn't work was because, player variable wasn't defined in your script.
  • Another reason was, you're trying to :FireClient to an instance that is null, THERE'S no plr parameter on your script, which again leads to another error.

##Solution:

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.

##Reccomendation:

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"

##Fixed Script:

(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!

####Thanks, JesseSong!

If you need any further help, don't hesitate to comment, or leave me a DM!

2 comments

0

Also, I don't think there's a need of putting returns in your script, I removed line 9 and added a player definition.

0

You can add a debounce to prevent spam

This discussion is preserved as it was. New answers, comments, and votes are closed.