Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why can't I fire a client?

Asked by 3 years ago
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

0
Is this a regular script or a localscript? AaronWilhelm -2 — 3y
0
server script antvvnio 40 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited by JesseSong 3 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your 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.

0
Use it because it works for me everytime. Dovydas1118 1495 — 3y
0
thats how i learnt to do it from someone antvvnio 40 — 3y
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. JesseSong 3916 — 3y
Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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!

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. JesseSong 3916 — 3y
0
You can add a debounce to prevent spam JesseSong 3916 — 3y

Answer this question