Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why gets everyplayer an amount of cash when one single player is clicking a tool?

Asked by 5 years ago

I think it has something to do with the remote event.

The server event script for the tool:


local liftSwordEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("LiftSword") liftSwordEvent.OnServerEvent:Connect(function(player,cashIncreaseValue) local cash = game.Players[script.Parent.Parent.Name].leaderstats.Cash for i, toolsInCharacter in pairs(script.Parent.Parent:GetChildren()) do if toolsInCharacter and toolsInCharacter:IsA("Tool") then local cashIncrease = toolsInCharacter:WaitForChild("CashIncrease") local multiplier = toolsInCharacter:WaitForChild("CashMultiplier") if multiplier then cash.Value = cash.Value + cashIncrease.Value * multiplier.Value end end end end)

The local script for the tool:

local sword = script.Parent
local liftSwordEvent = game.ReplicatedStorage.RemoteEvents:WaitForChild("LiftSword")

sword.Activated:Connect(function()
    if sword.Parent == game.Players.LocalPlayer.Character then
        liftSwordEvent:FireServer()
    end
end)

The leaderstats script

game.Players.PlayerAdded:Connect(function(player)
    local folder = Instance.new("Folder",player)
    folder.Name = "leaderstats"

    local cash = Instance.new("IntValue",folder)
    cash.Name = "Cash"
    cash.Value = 1500

    local weaponsOwned = Instance.new("Folder",player)
    weaponsOwned.Name = "weaponsowned"

    local sword1owned = Instance.new("BoolValue",weaponsOwned)
    sword1owned.Name = "Sword1owned"
    sword1owned.Value = false

    local sword2owned = Instance.new("BoolValue",weaponsOwned)
    sword2owned.Name = "Sword2owned"
    sword2owned.Value = false
end)

Thanks!

0
Then my answer is still valid since characters cannot equip more than 1 tool at the same time programmerHere 371 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Not sure what is up, tbh I never even looked. But you can simplify this code. First up why is there a for loop? Just to check if there is a tool equipped? Use :FindFirstChildOfClass here.

Also you do not need :WaitForChild since the objects will be loaded in already.

local liftSwordEvent =  game.ReplicatedStorage.RemoteEvents:WaitForChild("LiftSword")


liftSwordEvent.OnServerEvent:Connect(function(player)
    local cash = player.leaderstats.Cash
    local tool = player.Character:FindFirstChildOfClass("Tool")

    if tool then
        local cashIncrease = tool.CashIncrease
        local multiplier = tool.CashMultiplier
        cash.Value = cash.Value + cashIncrease.Value*multiplier.Value
    end
end)

And for your Activated listener... why test if they have the tool equipped when Activated ONLY fires if the tool is even equipped? How can you activate it without it even being equipped?

sword.Activated:Connect(function()
    liftSwordEvent:FireServer()
end)

0
I use the loop to get the values of the variables inside the sword so I can reference it to the script thebigwalkingdoor 0 — 5y
0
Well you did not specify what script.Parent.Parent is so I could not get your question programmerHere 371 — 5y
0
script.parent.parent leads to the character of the player. thebigwalkingdoor 0 — 5y
Ad

Answer this question