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

Why is my force field generator not working on servers?

Asked by 7 years ago

I have a brick where once you touch it, it gives you a forcefield. It works when I test it in studio but when I test it on a server it doesn't work at all.

function onTouched(hit)
    local character = game.Players.LocalPlayer.Character
    if character:FindFirstChild("ForceField") then else
        Instance.new("ForceField",character)
    end
end

script.Parent.Touched:connect(onTouched)

1 answer

Log in to vote
0
Answered by
legosweat 334 Moderation Voter
7 years ago

*Sorry for this being long, I just wanted to make sure you understood. *

The script you are using is invalid- and also the reason it works in studio but not on the server is due to studio treating scripts as localscripts. If you used a localscript, let me remind you localscripts do not work in Workspace, assuming this script is in a part- so put your code into a normal script.

Your function is set up and connected correctly, but it is the variable 'character' that makes the script error. This is because LocalPlayer can not be used in ServerScripts.

Now obviously, you do not want a random part to hit the brick, and you randomly get a forcefield. To prevent this, you need to check for a humanoid, so the function knows a player has touched it, and not a random part. So, this is where the 'hit' argument comes to play, as you see it in the functions brackets:

function onTouched(hit)

end
script.Parent.Touched:connect(onTouched)

When you connect this function to the .Touched event, the 'hit' argument will hold the object that touches the specified part. More information about this topic: http://wiki.roblox.com/index.php?title=API:Class/BasePart/Touched

You want to step on the part to obtain a forcefield, now when you step on the part, the .Touched will get what other part touched it. So we can obviously say that the limbs, etc. of the player will touch the part, in this so being 'hit' will hold the object that has touched the part.

Example: if your Lef tLeg hit the part, 'hit' would have the directory of LeftLeg in your players character.

So we'll track if a player touched it by getting the parent of the part (the character) and seeing if it has a Humanoid because in all characters, there is a Humanoid.

So here is the code so far:

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then

    end
end
script.Parent.Touched:connect(onTouched)

So now, to get the character of whom touched it, their character would be hit.Parent.

Make the character a variable so you can address it easily instead of hit.Parent, then add what you had before..

function onTouched(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local Character = hit.Parent
        if Character:FindFirstChild("ForceField") then 
            return
        else
                Instance.new("ForceField",character)
            end
    end
end
script.Parent.Touched:connect(onTouched)

HOPE this helped! I try my best..

Ad

Answer this question