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

Player event is triggering to all Players?

Asked by
Aozwel 71
3 years ago
Edited 3 years ago

Hi there, i’m pretty new to scripting and the mistake might be obvious but i’m unsure on what i need to change fix this,

The issue is bascailly when a Player clicks the Values/ “Equipped” text changes for all Players instead of the local player.

Local Script:

local player = game:GetService(“Players”).LocalPlayer
local RS = game:GetService(“ReplicatedStorage”)
local gui = script.Parent
– Items
local function UpdateKillEffect()
local Killeffects = player:WaitForChild(“KillEffects”)

for i, Button in pairs(gui.KillEffectScroll:GetChildren()) do
if Button:IsA(“ImageButton”) then
if Killeffects:FindFirstChild(Button.Name) then
Button.EquipStatus.Text = “Owned”
end
if player.EquippedKillEffect.Value == Button.Name then
Button.EquipStatus.Text = “Equipped”
end
if not Killeffects:FindFirstChild(Button.Name) then
Button.EquipStatus.Text = “Buy”
end
end
end
end

UpdateKillEffect()

player:WaitForChild(“EquippedKillEffect”):GetPropertyChangedSignal(“Value”):Connect(function()
UpdateKillEffect()
end)

for i, Button in pairs(gui.KillEffectScroll:GetChildren()) do
if Button:IsA(“ImageButton”) then
Button.Activated:Connect(function() print(Button.Name)
RS.Remotes.EquippedKillEffect:FireServer(“KillEffects”, Button.Name,Button.Price)
end)
end
end

RS.Remotes.Equip.OnClientEvent:Connect(function()
script.Sounds.Equip:Play()
end)

RS.Remotes.Bought.OnClientEvent:Connect(function()
script.Sounds.Buy:Play()
end)

RS.Remotes.Error.OnClientEvent:Connect(function()
script.Sounds.Error:Play()
end)

Server Script:

local RS = game:GetService(‘ReplicatedStorage’)
game:GetService(“Players”).PlayerAdded:Connect(function(Player)

local currentcoins = Player:WaitForChild(“coinstats”).Coins
local storage = RS.KillEffects

local EquippedKillEffect = Instance.new(“StringValue”)
EquippedKillEffect.Parent = Player
EquippedKillEffect.Name = “EquippedKillEffect”
EquippedKillEffect.Value = “None”

RS.Remotes.EquippedKillEffect.OnServerEvent:Connect(function(player, Type, Name, Price) print(“Fired Server”)
if not Name then return end
if not Type then return end
if type(Name) ~= ‘string’ then return end
if Type == “KillEffects” then
if player.KillEffects:FindFirstChild(Name) then
EquippedKillEffect.Value = (Name)
RS.Remotes.Equip:FireClient(player)
return
elseif not Player.KillEffects:FindFirstChild(Name) then
if currentcoins.Value >= Price.Value then
currentcoins.Value = currentcoins.Value - Price.Value
local newkilleffect = Instance.new(“BoolValue”)
newkilleffect.Name = (Name)
newkilleffect.Parent = player.KillEffects
RS.Remotes.Bought:FireClient(player)
return
elseif currentcoins.Value < Price.Value then
RS.Remotes.Error:FireClient(player)
end
end
end
end)
end)

Thanks,

1 answer

Log in to vote
1
Answered by
sayer80 457 Moderation Voter
3 years ago
  1. game:GetService(“Players”).PlayerAdded:Connect(function(Player) When there are 5 Players then the function will run 5 Times and each player will have a changed thing, as every function will be assigned to each player. You should just remove the function and define equipedkilleffect and that stuff inside the .OnServerEvent and you should get the equippedkilleffect with waitforchild then
local RS = game:GetService(‘ReplicatedStorage’)
local storage = RS.KillEffects

game:GetService(“Players”).PlayerAdded:Connect(function(Player) 
local EquippedKillEffect = Instance.new(“StringValue”)
EquippedKillEffect.Parent = Player
EquippedKillEffect.Name = “EquippedKillEffect”
EquippedKillEffect.Value = “None”
end)

RS.Remotes.EquippedKillEffect.OnServerEvent:Connect(function(player, Type, Name, Price) print(“Fired Server”)
local currentcoins = Player:WaitForChild(“coinstats”).Coins
local EquippedKillEffect = Player:WaitForChild(“EquippedKillEffect”)
if not Name then return end
if not Type then return end
if type(Name) ~= ‘string’ then return end
if Type == “KillEffects” then
if player.KillEffects:FindFirstChild(Name) then
EquippedKillEffect.Value = (Name)
RS.Remotes.Equip:FireClient(player)
return
elseif not Player.KillEffects:FindFirstChild(Name) then
if currentcoins.Value >= Price.Value then
currentcoins.Value = currentcoins.Value - Price.Value
local newkilleffect = Instance.new(“BoolValue”)
newkilleffect.Name = (Name)
newkilleffect.Parent = player.KillEffects
RS.Remotes.Bought:FireClient(player)
return
elseif currentcoins.Value < Price.Value then
RS.Remotes.Error:FireClient(player)
end
end
end
end)
Ad

Answer this question