This is my script for when a Zombie dies money is give to the player. RealMoney is an instance in the player. This script isn't working, How can this be fixed.
local zombie = game.Workspace:FindFirstChild("Zombie") local player = game.Players function zombie.Humanoid:died(d) while d == true do player.RealMoney = player.RealMoney + 5 end end
First off, when you are referencing the zombie in line one, this will only work if there is only one zombie in the whole workspace. Second, when you are referencing the player in line 2, you are only getting the folder where the players are, not all the players, and then when you check to see when the zombie died, you need to use .Died:Connect to connect it to a function to use, and you are supposed giving it an infinite amount of money over and over again. Then when you are referencing an instanced value in inside the player, you need to define the .Value afterwards. This might be what the final product would be:
local players = game.Players:GetPlayers() local zombie = game.Workspace:FindFirstChild("Zombie") zombie.Humanoid.Died:Connect(function() for i, v in pairs(players)do v.RealMoney.Value = v.RealMoney.Value + 5 end end)
This script would identify the zombie in game, and when it dies give every player 5 money, but it only works for one zombie, what I would suggest doing is making a local script inside the zombie itself, so when it dies it fires a remote event to the server telling it to only give it to a certain player, and this way it will work with multiple zombies.
EDIT: after looking at your comments, you could leave out the for i, v in pairs() because it's only a one person server, so it might look like this:
local players = game.Players:GetPlayers() local zombie = game.Workspace:FindFirstChild("Zombie") zombie.Humanoid.Died:Connect(function() players[1].RealMoney.Value = players[1].RealMoney.Value + 5 end)
It's probably easier to use RemoteEvents.
Here:
Add a RemoteEvent into ReplicatedStorage.
Local Script:
local zombie = game.Workspace:FindFirstChild("Zombie") while true do wait() if zombie:WaitForChild("Humanoid").Health <= 0 then game.ReplicatedStorage.RemoteEvent:FireServer() break end end
Server Script:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) player.RealMoney.Value = player.RealMoney.Value + 5 end)