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

What is wrong with this script? Error: "Workspace is not a valid member of Players"

Asked by 6 years ago
Edited 6 years ago
math.randomseed(tick())

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 < 50 then
 local sword1 = game.ServerStorage.Sword1:Clone()
 sword1.Parent = game.Players[player].Backpack

elseif random > 50 and random < 100 then
 local sword2 = game.ServerStorage.Sword2:Clone()
 sword2.Parent = game.Players[player].Backpack 
end 

script.Parent:Destroy()
end)

****Error Line 15 and 11

0
Make sure you check that the hit is a player before you use it as one. My guess is that whatever your object touched is parented to the workspace, which doesn't seem to be your intention. ThatPreston 354 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

If the first thing that hits your script.Parent is not a player your script will break in some way. What you need to do is put in an if statement that checks to see if it is a player hitting your part or whatever it is like so:

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        -- code here
    end
end)

Hope this helps. Have a good day scripting.

0
It worked, Thank you so much Phlegethon!!! KingCheese13 21 — 6y
0
No problem User#21908 42 — 6y
Ad

Answer this question