I’m working on a restocker job script. Basically, you click the back of the truck, pick out a box, then put the box to a little pad. The box should then disappear. My issue is that the box is not disappearing.
Here’s my code. I am not posting a hierarchy since the script is just a child of the pad.
script.Parent.Touched:connect(function(Hit)
local player = game.Players:GetPlayerFromCharacter(Hit.Parent.Parent)
print(Hit.Parent.Name)
local char = Hit.Parent
if Hit.Name == ‘JobBox’ then
print(‘BOXHIT’)
script.Parent.Parent.Anim.Value = ‘false’
script.Parent.Parent.Taken.Value = ‘false’
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 20
Hit:Destroy()
It could be that your box model is you know, a model. When the .Touched event fires, the variable Hit will be equal to the exact part that hit, not the entire model. If your box does use a Model instance to keep things tidy, you are dismissing it in many places.
Secondly, you seem to be setting booleans using full on strings. If the ValueContainer is of the type Boolean, you should get rid of the ''s surrounding the false’s.
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(Hit)
local player = Players:GetPlayerFromCharacter(Hit.Parent.Parent)
print(Hit.Parent.Name)
local char = Hit.Parent
if Hit.Name == 'JobBox' then
print('BOXHIT')
script.Parent.Parent.Anim.Value = 'false'
script.Parent.Parent.Taken.Value = 'false'
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 20
Hit:Destroy()
end
end)
I neatened up your script a bit to make it easier for everyone who’s helping.
Also, I noticed you used script.Parent (I’m assuming you have a script in every single part), this is not how it should be done. Instead, you should have a singular script in ServerScriptService which manages all of the boxes automatically.
Upon analyzing and rewriting the script, it looks like this:
script.Parent.Touched:Connect(function(hit)
local char = hit.Parent
local player = game:GetService("Players"):GetPlayerFromCharacter(char.Parent)
print(char.Name)
if hit.Name == "JobBox" then
print("BOXHIT")
script.Parent.Parent.Anim.Value = false -- fixed
script.Parent.Parent.Taken.Value = false -- fixed
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 20 -- risky
Hit:Destroy()
end
end)
Additionally, you’re parenting the “box” to the character which is already somewhat complicated. In my case of work, I would use CollectionService to tag the boxes with respective player name by the player “owns” it from the beginning.
I could not identify where the script is placed, but provided hit is a “box”, I think it is the box collector. It is not wrong to place it in an object but it would be easier to sort out by rewriting this to a defined function and connect the Touched event to it. Unless you plan to only use one collector, that is.