Need Help with a script!

i’ve been trying to make it where if my tool touches part it will add a value to Storage

function onTouched(part,Players)
 local h = part.Parent:FindFirstChild("Humanoid")
 if (h~=nil) then
  local thisplr = game.StarterPack["Basic Net"].Handle
  if (thisplr~=nil) then
	local BackPackF = Players.BackpackF
	local Storage = BackPackF.Storage
	local MaxStorage = BackPackF.MaxStorage
	Storage.Value = Storage.Value +1
	wait(.2)
	  script.Parent:remove()
    end
   end
  end


script.Parent.Touched:connect(onTouched)

https://gyazo.com/edd5f8b2dee7048be5192ffb3de9e7bc

1 Like

You’re defining thisPlr from game.StarterPack

game.StarterPack is not a parent of players. What is inside of starter pack is cloned into the Backpack child of a player object.

Also, there is no players parameter of .Touched

2 Likes

Small note, :remove() is deprecated, use :Destroy()


StarterPack is global. The player has something called Backpack.

Storage management is strange here. Why did you use Players for storing? Anyways, try using ServerStorage instead, the canonical way to store.

Assume this was a normal script parented under handle.

A few questions beforehand

Is thisplr supposed to be equal to the current player using the tool? If so you can just use game.Players:GetPlayerFromCharacter(h.Parent). That will get the Player from the Character found.

Where is the Storage located? IS BackPackF located in the Player? So whenever the player hits the part, they “Collect” it in a way?

Also, I believe Players is going to be nil as the Touched only outputs the part touched and not Players but I may be wrong I have never seen that before.

Edit: Where is this script located? Is this under the part being collected or under the tool?

Well i modified my script to this

local BasicNet = game.StarterPack["Basic Net"].Handle
local Bread = game.Workspace.Bread
script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        Swing = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Swing)
        Swing:Play()
if Swing:Play()then
	Bread.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild(BasicNet) then
			game.Players.LocalPlayer.BackpackF.Storage.Value =  game.Players.LocalPlayer.BackpackF.Storage.Value + 1
		end
	end)
end
    end)
end)

script.Parent.Unequipped:Connect(function()
      Swing:Stop()
end)

any personal thoughts on it!

i get no errors still :frowning:

This is a local script inside the tool

Hey there.

LocalScripts changing values do not affect the server at all. Thus you have to split the script into one for server and one for client.

More about why LocalScripts cannot change values and replicate on server is here.

1 Like

I don’t think you need if Swing:Play() then you can just use Bread.Touched. Here is an idea though for you that can probably make this entire thing a lot easier. Create a folder within Workspace that holds all the bread in it. Create a new script under ServerScriptService and in that script do this. Use a For Loop to loop through the folder holding your bread. This will look a little like this

local bread = game.Workspace:WaitForChild("Bread")
for i,bread in pairs(bread:GetChildren()) do

end

You can then use the .touched with the bread within this loop

for i,bread in pairs(bread:GetChildren()) do
    bread.Touched:connect(function(touched)
	
    end)
end

Then from there, you should be able to receive the tool that touched it, make sure it is a tool and not a player walking on the bread. From the Tool, you can receive the player, and with the player, you can change the stat within the Player’s backpack. Changing these important values on the client is dangerous and assuming you want the server to see how much bread the player has you should be changing the value on the server.

Over in the LocalScript located under the tool you can use

local playerSwinging = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
	if not playerSwinging then
		playerSwinging = true
	
        Swing = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Swing)
        Swing:Play()
		Swing.Stopped:wait()
		playerSwinging = false
	end
end)
end)

This will play the animation and also has the debounce. Id check debounces out too, those are pretty important :slight_smile:

2 Likes

no the if then Swing:Play() then was for if animation was played

That’s the incorrect usage, please consider using AnimationTrack.IsPlaying bool instead.


Swing is an AnimationTrack, therefore you can type if Swing.IsPlaying then.

1 Like

i did this

in the ServerScriptServiceScript and this one for tool !