Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Cola tool not doing what script says to do (Slurp, +1 leaderstat point) ?

I made a Cola tool that whenever you equip and use it, it plays a slurp sound and adds 1 point to your leaderstat. For some reason, it does not work though.

Here is the script:

local slurp = game.StarterPack.Soda.Slurp
local tool = script.Parent
local Lplayer  = game.Players.LocalPlayer
local leaderstat = Lplayer.Leaderstats
print(leaderstat)

  function slurp()
   slurp:Play()
   wait(0.5)
   print("Slurping")
end
	
  local function onActivate()
   slurp()
   wait(0.1)
   print("Adding")
   leaderstat.Points + 1
   print("Added")
end

tool.Activated:Connect(onActivate)

"Slurp" is a roblox library sound, though im not sure if you can play it as a function. Any help and explanation on how to fix it will be very appreciated!

1 answer

✓ Accepted answer

Don't name your functions and your variables the same thing. Rename your slurp function to something else. Also, you have to use the assignment operator to add to a value. This is also not a good idea because you're getting LocalPlayer from the script, so I'm assuming you're using a LocalScript. Playing animations or changing value on the client (LocalScript) does not replicate over to the Server, so only the player using the cola will see the effect, or hear it, or even the addition to their leaderstat. If you want the whole server to see it, I'd recommend using a ServerScript. You're also trying to play the audio from StarterPack, not in the actual tool itself.

Fixed LocalScript:

local tool = script.Parent
local slurp = tool.Slurp -- see below as to why i changed this
local Lplayer  = game.Players:GetPlayerFromCharacter(tool.Parent) or tool.Parent.Parent;
-- this is because ServerScripts don't have access to LocalPlayer, so we get it using their character OR if it's in their backpack, we go up twice
local leaderstat = Lplayer.Leaderstats
print(leaderstat)

  function playSlurp()
   slurp:Play()
   wait(0.5)
   print("Slurping")
end

  local function onActivate()
   playSlurp()
   wait(0.1)
   print("Adding")
   leaderstat.Points.Value = leaderstats.Points.Value + 1 -- adding to it's current value
   print("Added")
end

tool.Activated:Connect(onActivate)

Fixed ServerScript:

local tool = script.Parent
local slurp = tool.Slurp -- Changed this so that it gets the audio IN the tool and can play it
local Lplayer  = game.Players.LocalPlayer
local leaderstat = Lplayer.Leaderstats
print(leaderstat)

  function playSlurp()
   slurp:Play()
   wait(0.5)
   print("Slurping")
end

  local function onActivate()
   playSlurp()
   wait(0.1)
   print("Adding")
   leaderstat.Points.Value = leaderstats.Points.Value + 1 -- need the value, not the object
   print("Added")
end

tool.Activated:Connect(onActivate)

2 comments

0

How would I put in the ServerScript?

0

I found a error in the code. It says local leaderstat = Lplayer.Leaderstats, but leaderstats should ALWAYS be called "leaderstats". No typos or caps.

This discussion is preserved as it was. New answers, comments, and votes are closed.