Hi I just found out that the code i’ve written below works on a localscript but the animation and leaderstats won’t show for other players, so how do I transfer it to a server script? I tried but it gives me no error and it doesn’t work. I know it probably has something to do with getting the player, but I don’t know how to do that and it gave it didn’t work when I did "Local player = game:GetService(“Players”)
local player = game.Players.LocalPlayer
local Pickaxe = script.Parent
local Handle = player.Backpack.StonePickaxe.Handle or player.Character.StonePickaxe.Handle
local Animation = Pickaxe.Animation
local HitPart = script.Parent.Pickaxe.HitPart
local debounce = false
local Rock = game.Workspace.Rock
local mouse = player:GetMouse()
Pickaxe.Activated:Connect(function()
if not debounce then
debounce = true
local Character = Pickaxe.Parent
local Humanoid = Character.Humanoid
local AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Animation)
AnimationTrack:Play()
if mouse.Target == Rock then
local StoneValue = player.leaderstats.Stone
StoneValue.Value += 1
end
wait(1)
debounce = false
end
end)
First, ensure the animation is uploaded by you and not someone else, or it won’t show up in the first place. It has to be the owner of the group or the owner of the game.
Second, use local player = game:GetService(“Players”).LocalPlayer
This will make sure that the service is loaded.
Third, leaderstats should be in a server script called ‘leaderstats’ and make sure you followed a tutorial correctly as naming is critical.
Fourth, that script should be in a local script inside the tool, and I’m not sure what the animation deal is and why it is only client-sided, you will have to figure that out yourself by maybe using a remote event to cast the animation or maybe someone else commenting will know.
Lastly, I can’t really help you much on anything else, I barely know how to make a working game but you should still take my advice. Would you also mind sharing with me what the error you received is?
game:GetService("Players").LocalPlayer is only usable on the client-side
Animation doesn’t show for others?!
Thats weird as any animation played on client-side is replicated to others… (Source)
Consider this:
For the leaderstats you should change and create it through the server to replicate to others and to prevent exploitation of it. This can be done using a RemoteEventDoc that tells the server to change the values with the passed mouse target argument.
You can also detect the activation of a tool using a server script but with different techniques.
As an example of the RemoteEvent:
-- Local Script (Client-Side Script)
local player = game.Players.LocalPlayer
local Pickaxe = script.Parent
local Handle = player.Backpack.StonePickaxe.Handle or player.Character.StonePickaxe.Handle
local Animation = Pickaxe.Animation
local HitPart = script.Parent.Pickaxe.HitPart
local debounce = false
local Rock = game.Workspace.Rock
local mouse = player:GetMouse()
local RemoteEvent = -- Your remote
Pickaxe.Activated:Connect(function()
if not debounce then
debounce = true
local Character = Pickaxe.Parent
local Humanoid = Character.Humanoid
local AnimationTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Animation)
AnimationTrack:Play()
if mouse.Target == Rock then
RemoteEvent:FireServer(mouse.Target) --| Fires the event with the current mouse target for some kind of validation
--local StoneValue = player.leaderstats.Stone
--StoneValue.Value += 1
end
wait(1)
debounce = false
end
end)
-- Server Script:
local Players = game:GetService("Players")
local RemoteEvent = --Your Event
local Debounces: {[Player]: boolean} = {--[[ Debounce Validation For Every Player ]]}
RemoteEvent.OnServerEvent:Connect(function(Player, MouseTarget)
if Debounces[Player] ~= nil and Debounces[Player] or not Player.Character then return end
Debounces[Player] = true
if MouseTarget and MouseTarget == workspace:FindFirstChild("Rock") then
local StoneValue = Player.leaderstats.Stone
StoneValue.Value += 1
end
task.wait(1)
Debounces[Player] = false
end)
Players.PlayerAdded:Connect(function(Player)
Debounces[Player] = false
end)
Thank you, tho when it is on a server script I can equip the pickaxe but the animation doesn’t run and I don’t +1 in the leaderstats when clicking. And sorry the animation does work in a local script, the leaderstats is just shown 0 for everyone else and they can’t see the pcikaxe if it’s in localscript.