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

How do I stop this script from breaking after being touched by anything other than a player?

Asked by 5 years ago

This script is for a random weapons crate in my game, however I also have aliens roaming around. When one of these aliens runs into a weapons crate the script instantly breaks with the response : "Alien is not a valid member of Players." How would I make it so that if anything other than a player touches it, the script doesn't break?

script.Parent.Touched:connect(function(hit)
 script.Disabled = true

local player = hit.Parent.Name
local random = math.random(1,100)

if random > 0 and random < 23 then
 local laserSword = game.ServerStorage.LaserSword:Clone()
 laserSword.Parent = game.Players[player].Backpack

elseif random > 23 and random < 61 then
 local sCAR = game.ServerStorage.SCAR:Clone()
 sCAR.Parent = game.Players[player].Backpack 

elseif random > 61 and random < 100 then
 local weapon7 = game.ServerStorage.Weapon7:Clone()
 weapon7.Parent = game.Players[player].Backpack 
end

script.Parent:Destroy()
end)

    end

end)

2 answers

Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
5 years ago

Use Players:GetPlayerFromCharacter() and check if it returns nil or not.

local ev
ev = script.Parent.Touched:Connect(function(hit)
    local plr = hit.Parent.ClassName == "Model" and hit.Parent:FindFirstChildOfClass("Humanoid") and game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if not plr then
        return --abort if plr is nil
    end
    ev:Disconnect()

    local random = math.random(1,100)

    if random > 0 and random < 23 then
        local laserSword = game:GetService("ServerStorage").LaserSword:Clone()
        laserSword.Parent = plr.Backpack

    elseif random > 23 and random < 61 then
        local sCAR = game:GetService("ServerStorage").SCAR:Clone()
        sCAR.Parent = plr.Backpack 

    elseif random > 61 and random < 100 then
        local weapon7 = game:GetService("ServerStorage").Weapon7:Clone()
        weapon7.Parent = plr.Backpack 
    end

    script.Parent:Destroy()
end)
0
Thanks so much it works! KingCheese13 21 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

here is an easy three lines:

if not game.Players:GetPlayerFromCharacter(hit.Parent)
    return
end

now, if you want it IN THE CODE, here:

script.Parent.Touched:connect(function(hit)
    -------- CHECKING FOR PLAYER... --------
    if not game.Players:GetPlayerFromCharacter(hit.Parent)
        return
    end
    -- If the script continues from here, the player is not nil.
 script.Disabled = true

local player = hit.Parent.Name
local random = math.random(1,100)

if random > 0 and random < 23 then
 local laserSword = game.ServerStorage.LaserSword:Clone()
 laserSword.Parent = game.Players[player].Backpack

elseif random > 23 and random < 61 then
 local sCAR = game.ServerStorage.SCAR:Clone()
 sCAR.Parent = game.Players[player].Backpack 

elseif random > 61 and random < 100 then
 local weapon7 = game.ServerStorage.Weapon7:Clone()
 weapon7.Parent = game.Players[player].Backpack 
end

script.Parent:Destroy()
end)

    end

end)

If you want to help us here in Masterful Studios (because we NEED it!), please join us here: http://www.roblox.com/My/Groups.aspx?gid=3326074

Answer this question