Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

1 comment

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.

1 answer

✓ Accepted answer

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.

2 comments

0

It worked, Thank you so much Phlegethon!!!

0

No problem

This discussion is preserved as it was. New answers, comments, and votes are closed.