Hi, I am a new developer and I have started to make a couple games. One of my games I want to make loot crates that randomly spawn and give random loot of which have a certain drop chance. But the script that I have wrote it will work but not give you any loot. (I have not yet made the random spawn chances for the crates spawning) I don’t know if it’s that I have the weapons in the wrong place or if it’s just the script. But here is the script:
I see 2 conflicting issues in this script, Local Player and ServerStorage. If its a client script they cannot access server storage. If it’s a server script, the server doesn’t have a local player.
A script can’t access to local player this in case that your code is in a script.
A localscript can’t access to the ServerStorage try to use the ReplicatedStorage this in case that your code is in a localscript.
Can you show the type of script that you are using?
I am using the regular script.
Do I need to use the local script?
I would use a localscript, now you wonder how I know the player who touches the block, you can use this.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:FindFirstChild(hit.Parent.Name)
sword1.Parent = player.Backpack
print(player)
end
end)
It print the name of the player that touch the part
You can’t edit the code. I’m on mobile
What do I change my local player = hit.parent to
and what do I change my game.Players.LocalPlayer.Backpack local player in that to.
A script can’t acess to LocalPlayer so you can’t use it.
Again, I am new to all this so don’t get triggered if somethings wrong here but here’s what I have so far:
math.randomseed(tick())
script.Parent.Touched:connect(function(hit)
script.Disabled = false
local player = hit.Parent
local random = math.random(1,100)
if random > 0 and random < 50 then
local sword1 = game.ReplicatedStorage.sword1:Clone()
sword1.Parent = game.Players.LocalPlayer.Backpack
elseif random > 50 and random < 100 then
local pistol1 = game.ReplicatedStorage.Pistol1:Clone()
pistol1.Parent = game.Players.LocalPlayer.Backpack
end
script.Parent:Destroy()
end)
if hit.Parent:FindFirstChild(“Humanoid”) then
local player = game.Players:FindFirstChild(hit.Parent.Name)
print(player)
end
math.randomseed(tick())
script.Parent.Touched:connect(function(hit)
if hit.ParentFindFirstChild(“Humanoid”) then
script.Disabled = false
local player = game.Players:FindFirstChild(hit.Parent.Name)
local random = math.random(1,100)
if random > 0 and random < 50 then
local sword1 = game.ReplicatedStorage.sword1:Clone()
sword1.Parent = player.BackPack
elseif random > 50 and random < 100 then
local pistol1 = game.ReplicatedStorage.Pistol1:Clone()
pistol1.Parent = player.Backpack
end
script.Parent:Destroy()
end
end)
If you have your tools in ServerStorage change all the ReplicatedStorage into ServerStorage. This is in a regular script.
I honestly don’t know what to do, it still is not working. I don’t know what’s going on.
The weapons are in replicatedstorage and I did the script like you did.
Ok, send me all your script maybe is something at the top.
The script marks an error in the output?
No, not from what I have seen in the output
Dude- you see line 4? You forgot to put a period between parent and find first child
Whoops, didn’t even notice that. Thank you
Still does not work
I saw that you are using a localscript change it into a script