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

Random forcefield spawning in my game?

Asked by
R_alatch 394 Moderation Voter
8 years ago

So, I have this code on multiple parts that checks if a player already has a forcefield. If they don't, they are given one. Then, on another part I have a code that checks if a player has one, then removes it. But, sometimes I have a random forcefield spawning at 0, 0, 0. I don't know why this is. Any ideas?

Here are my 2 different codes:

script.Parent.Touched:connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if not player.Character:FindFirstChild("ForceField") and player.Character.Humanoid then
            local ff = Instance.new("ForceField", player)
        end
    end
end)

2 answers

Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
8 years ago

You probably need to check if hit.Parent is a character. You can do this by checking for a Humanoid, or by using :GetPlayerFromCharacter().

script.Parent.Touched:connect(function(hit)
    if game.Players:GetPlayerFromCharacter(hit.Parent) ~= nil then
        if not hit.Parent:FindFirstChild("ForceField") then
            local ff = Instance.new("ForceField", hit.Parent)
        end
    end
end)

Hope this helped.

Ad
Log in to vote
1
Answered by
hozann 75
8 years ago

I suggest that if you don't want a forcefield occur you do the following; Select your spawnlocation Go to its properties and find the Forcefield tab, make sure the duration is 0, or however long you want. If it is 0 it won't give the player that spawns there a forcefield.

Answer this question