Error with Code

I don’t know what I did wrong here:

player.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	humanoid.Died:Connect(function()
		player.msmMoneyValue.Value = 0
		game.StarterGui.RobbingGui.Enabled = false
	end)
end)

But this was the error I got:
image

1 Like

How Exactly are you getting the Player, Is it a Server Script?

1 Like

Yes it is. I also put the script in ServerScriptService.

1 Like

Make sure the variable player is defined in your code.

2 Likes

You never defined the “player” variable

Edit: Someone said the same thing right as I posted; try the code below;

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        local humanoid = character:WaitForChild("Humanoid")
        humanoid.Died:Connect(function()
            player.msmMoneyValue.Value = 0
            game.StarterGui.RobbingGui.Enabled = false
        end)
    end)
end)
3 Likes

attempt to index nil with [index]
is caused because there is no such value named [index] in the current context.

for an example,

mrbeast.marry("me") -- attempt to index nil with "marry"

except if i define it liek this:

local mrbeast = {marry = print} -- adds the table named "mrbeast" to the current context
mrbeast.marry("me") -- outputs "me"

I think you got the wrong topic, lol

Nope, if you were pointing to the mrbeast code, it was only an example. let the op figure it out and learn :grin:

1 Like

Btw, hope you’re a woman, or that would be, uh, susy baka lol

2 Likes

But now I got this:

1 Like

you cannot index LocalPlayer from a server script, because the server side is not driven by a player (it will return nil if you access localplayer from the server)

if you want to index LocalPlayer, use it in a localscript.

1 Like

also, if you change a player’s UI in StarterGui, it wont replicate to anyone, except to the server it self. so change the player’s ui by accessing the player’s “PlayerGui” folder

1 Like

So make it a local script instead of a server script?

1 Like

copy paste your script from the image above, then i’ll modify it

1 Like

Use the </> option Jdlranger

1 Like
local player = game.Players.LocalPlayer
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")
		humanoid.Died:Connect(function()
			player.msmMoneyValue.Value = 0
			game.StarterGui.RobbingGui.Enabled = false
		end)
	end)
end)
1 Like

doesnt seem the same?


1 Like

Do you at least know a code that detects when the player dies?

1 Like

Nevermind, I found a solution. Thanks for the help anyway guys!

1 Like

Probably should be something like this:

Code

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
		local humanoid = character:FindFirstChild("Humanoid")
		humanoid.Died:Connect(function()
			player.msmMoneyValue.Value = 0
			player.PlayerGui.RobbingGui.Enabled = false
		end)
	end)
end)
1 Like