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

Should I use while loop in this script, because this script isn't working?

Asked by 4 years ago

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
0
You have to find out who killed the zombie Nguyenlegiahung 1091 — 4y
0
It is a 1 man server, would I use game.localplayer ? FluffySheep46209 369 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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)
0
Thx FluffySheep46209 369 — 4y
0
Happy to help! Let us know if you come across anymore problems/questions and we'll do our best to answer them! jediplocoon 877 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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)
0
What should I name the remote event FluffySheep46209 369 — 4y
1
You can name it anything, it doesn't matter. You just gotta make sure to replace "RemoteEvent" in the script with the name you're changing it to. An example of the name could be "GiveMoneyOnZombieDeath" or something like that. xInfinityBear 1777 — 4y

Answer this question