Script not working

i’ve been trying to make it where if tool animation is playing and if tool hits the part while animation playing then it adds a value to an IntValue
Local script inside of tool

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

script inside of ServerScriptService

local tool = game.StarterPack["Basic Net"].Handle
local bread = game.Workspace:WaitForChild("Bread")
for i,bread in pairs(bread:GetChildren()) do
	bread.Touched:Connect(function(touched)
		if touched.Parent == tool then
			game.Players.LocalPlayer.BackpackF.Storage.Value =  game.Players.LocalPlayer.BackpackF.Storage.Value + 1
		end
	end)
end

No errors in output
Does anybody know how to add values by using server instead of client

1 Like

Replace your server code with this:

local tool = game.StarterPack["Basic Net"].Handle
local bread = game.Workspace:WaitForChild("Bread")
for i,bread in pairs(bread:GetChildren()) do
	bread.Touched:Connect(function(touched)
		if Player.Character:FindFirstChild(tool.Name) then
			game.Players.LocalPlayer.BackpackF.Storage.Value =  game.Players.LocalPlayer.BackpackF.Storage.Value + 1
		end
	end)
end
1 Like

hey but look,play isnt defined

that is very correct that i forgot to create Player

local tool = game.StarterPack["Basic Net"].Handle
local bread = game.Workspace:WaitForChild("Bread")
for i,bread in pairs(bread:GetChildren()) do
	bread.Touched:Connect(function(touched)
		local Player = game.Players:GetPlayerFromCharacter(touched.Parent)
		if Player then
			if Player.Character:FindFirstChild(tool.Name) then
				Player.BackpackF.Storage.Value = Player.BackpackF.Storage.Value + 1
			end
		end
	end)
end

Also I just noticed that youre using LocalPlayer in a ServerScript, which won’t work. Instead you need to get Player from touch, which I did in this presumably (now) current script.

1 Like

Will this work?

local AddValue = require(game.ServerScriptService.BackpackValue)
local bread = game.Workspace.Bread
local Players = game.Players.LocalPlayer
local Character = Players.Character or Players.CharacterAdded:Wait();
local tool = script.Parent
local playerSwinging = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
	if not playerSwinging then
		playerSwinging = true
        local Swing = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Swing)
        Swing:Play()
		Swing.Stopped:wait()
		playerSwinging = false
		for i,bread in pairs(bread:GetChildren()) do
	      bread.Touched:Connect(function(touched)
		    if Players.Character:FindFirstChild(tool.Name) then
             AddValue.AddValue(Players)
		end
	end)
end
	end
end)
end)

Module script

local AddValue = {}

 function AddValue()
	game.Players.LocalPlayer.BackpackF.Value = game.Players.LocalPlayer.BackpackF.Value +1
end
return AddValue

Your use of a module script is useless, and also won’t work.

Remove local AddValue = require(game.ServerScriptService.BackpackValue)

And replace AddValue.AddValue(Players) with game.Players.LocalPlayer.BackpackF.Value = game.Players.LocalPlayer.BackpackF.Value +1

2 Likes
local bread = game.Workspace.Bread
local Players = game.Players.LocalPlayer
local Character = Players.Character or Players.CharacterAdded:Wait();
local tool = script.Parent
local playerSwinging = false
script.Parent.Equipped:Connect(function(Mouse)
Mouse.Button1Down:Connect(function()
	if not playerSwinging then
		playerSwinging = true
        local Swing = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Swing)
        Swing:Play()
		Swing.Stopped:wait()
		playerSwinging = false
		for i,bread in pairs(bread:GetChildren()) do
	      bread.Touched:Connect(function(touched)
		    if Players.Character:FindFirstChild(tool.Name) then
          game.Players.LocalPlayer.BackpackF.Storage.Value = game.Players.LocalPlayer.BackpackF.Storage.Value +1
		end
	end)
end
	end
end)
end)

i did but it wont add value,and no errors in output

Im assuming that LocalPlayer.BackpackF.Storage.Value is a leaderstats value?

I don’t for sure, but it might need to be done on the server via a RemoteEvent.

1 Like

You should slap some print()'s in here so you can see where the script is getting stopped.

I feel like you’re mixing up what the server has to do here and what the client has to do.
Let’s write some pseudocode for what the server has to do:

--[[
When bread is touched
    check if a player touched the bread
        check if player's tool was activated
            add storage    
]]

Now, the pseudocode for what the client has to do:

--[[
When the tool is activated
   play animation
]]

Now let’s try coding our pseudocode:

Server

local loaf = workspace.Bread:GetChildren()

for _, bread in pairs(loaf) do
    bread.Touched:Connect(function(part)
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if not player or not player:FindFirstChildOfClass("Tool") then return end 
        -- Keep in mind that this does not check for a specific tool, feel free to switch to a :FindFirstChild()
        -- Check if player's tool is currently activated [INCOMPLETE]
        if activated then
            player.BackpackF.Storage.Value = player.BackpackF.Storage.Value + 1
        end
    end)
end

Client

local player = game.Players.LocalPlayer
local tool = script.Parent
local swinging = false

local swing = player.Character.Humanoid:LoadAnimation(script.Parent.Swing)

tool.Activated:Connect(function()
    swinging = not swinging -- true turns false and false turns true
    if swinging then
        swing:Play()
        swing.Stopped:Wait()
        swinging = false
    end
end)

So as of now, our scripts can:

  • Play the animation
  • Check if bread was touched and link it to a player
  • Add storage

What’s missing is the check if player’s tool was activated part. We can achieve this by checking if the animation is playing:

local function isAnimPlaying(player, animName)
    local tracks = player.Character.Humanoid:GetPlayingAnimationTracks()
    for _, track in pairs(tracks) do
        if track.Animation.Name == animName then return true end
    end
    return false
end

Incorporating this into our previous server code:

local debounce = {}
local loaf = workspace.Bread:GetChildren()

local function isAnimPlaying(player, animName)
    local tracks = player.Character.Humanoid:GetPlayingAnimationTracks()
    for _, track in pairs(tracks) do
        if track.Animation.Name == animName then return true end
    end
    return false
end

for _, bread in pairs(loaf) do
    debounce[bread] = {}
    bread.Touched:Connect(function(part)
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if not player or not player:FindFirstChildOfClass("Tool") then return end 
        if isAnimPlaying(player, "Swing") and not debounce[bread][player] then
            player.BackpackF.Storage.Value = player.BackpackF.Storage.Value + 1
            debounce[bread][player] = true
            wait(1)
            debounce[bread][player] = false
        end
    end)
end

That’s it, our scripts can together:

  • Play an animation
  • Check if a player touches the bread and if their tool is currently playing the animation and reward storage if so.

Final Scripts


Server

local debounce = {}
local loaf = workspace.Bread:GetChildren()

local function isAnimPlaying(player, animName)
    local tracks = player.Character.Humanoid:GetPlayingAnimationTracks()
    for _, track in pairs(tracks) do
        if track.Animation.Name == animName then return true end
    end
    return false
end

for _, bread in pairs(loaf) do
    debounce[bread] = {}
    bread.Touched:Connect(function(part)
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if not player or not player:FindFirstChildOfClass("Tool") then return end 
        if isAnimPlaying(player, "Swing") and not debounce[bread][player] then
            player.BackpackF.Storage.Value = player.BackpackF.Storage.Value + 1
            debounce[bread][player] = true
            wait(1)
            debounce[bread][player] = false
        end
    end)
end

Client

local player = game.Players.LocalPlayer
local tool = script.Parent
local swinging = false

local swing = player.Character.Humanoid:LoadAnimation(script.Parent.Swing)

tool.Activated:Connect(function()
    swinging = not swinging
    if swinging then
        swing:Play()
        swing.Stopped:Wait()
        swinging = false
    end
end)

This whole script is based on a lot of assumptions when it comes to what exactly you want to achieve, please tell me if this isn’t what you were aiming to do. If it was, mark this as the answer :wink:

4 Likes

Not its not leaderstats,itts a foldder in player with IntValue’s