I have this script for a loot crate that will give a random weapon if touched.
01 | math.randomseed(tick()) |
02 |
03 | script.Parent.Touched:Connect( function (hit) |
04 | script.Disabled = true |
05 |
06 | local player = hit.Parent.Name |
07 | local random = math.random( 1 , 100 ) |
08 |
09 | if random > 0 and random < 50 then |
10 | local ar = game.ServerStorage.AR:Clone() |
11 | ar.Parent = game.Players [ player ] .Backpack |
12 |
13 | elseif random > 50 and random < 100 then |
14 | local crossbow = game.ServerStorage.Crossbow:Clone() |
15 | crossbow.Parent = game.Players [ player ] .Backpack |
16 | end |
17 |
18 | script.Parent:Destroy() |
19 | end ) |
The problem is, it didn't work. It says: "Touched is not a valid member of Part"
Please help, thank you! <3
Hello. You'd need an if statement checking if the player isn't nil from the :GetPlayerFromCharacter()
function. It gets that player from its character, hi.Parent. Try this:
01 | math.randomseed(tick()) |
02 |
03 | script.Parent.Touched:Connect( function (hit) |
04 | local player = game.Players:GetPlayerFromCharacter(hit.Parent) |
05 |
06 | if player then |
07 | script.Disabled = true |
08 |
09 | local random = math.random( 1 , 100 ) |
10 |
11 | if random > 0 and random < 50 then |
12 | local ar = game.ServerStorage.AR:Clone() |
13 | ar.Parent = player.Backpack |
14 |
15 | elseif random > 50 and random < 100 then |
Please accept and upvote this answer if it helped.