Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

This leaderboard and text when you die script works in sutdio test but not when I play it normally?

When I use this in studio, it works. When I play the game normally, the leaderboard is not there and it does nothing when I die. Please tell me what I did wrong, thank you.

Player = game.Players.LocalPlayer
game.Players.PlayerAdded:connect(function(Player)
	local stats = Instance.new("IntValue")
	stats.Parent = Player
	stats.Name = "leaderstats"

	local Points = Instance.new("IntValue")
	Points.Parent = stats
	Points.Name = "Pointz"

	deaths = Instance.new("IntValue")
	deaths.Parent = stats
	deaths.Name = "Deaths"

	game.Players.LocalPlayer.CharacterAdded:connect(function()	
		game.Players.LocalPlayer.Character.Humanoid.Died:connect(function()
			deaths.Value = deaths.Value + 1
			local deathGui = Instance.new("ScreenGui")
			deathGui.Parent = game:GetService("Players").LocalPlayer.PlayerGui
		
			local deathText = Instance.new("TextBox")
			deathText.Parent = deathGui
			deathText.Text = "YOU SUCK, "..game.Players.LocalPlayer.Name
			deathText.FontSize = "Size96"
			deathText.Position = UDim2.new(0.5,0,0.5,0)
			deathText.BackgroundTransparency = 1
			deathText.TextColor3 = Color3.new(255,255,255)
			wait(5)
			deathGui:Remove()
		end)
	end)
end)

4 comments

0

Is it in a normal script or a local script?

0

As you are using "LocalPlayer" I assume that this is a local script. You are trying to mix server events with local ones. You need to 1st use a server sctipt for adding the leaderstats nad re-think how the gui side will work.

0

This is a normal script, not a local script

0

Only local script have acces to localplayer so thats half of the problem....

1 answer

✓ Accepted answer

Alright, so you've made it clear you're not using a localscript, so here's your problem.

The term (when accessing the "Players" service) "LocalPlayer" refers to the client's player. This can only be used when using a localscript, server scripts do not have a "LocalPlayer". Here's how the script should go.

game.Players.PlayerAdded:connect(function(Player) --This listens for when a player is added and defines the `argument ` "Player" as the aforementioned player who joined.
    local stats = Instance.new("IntValue")
    stats.Parent = Player
    stats.Name = "leaderstats"

    local Points = Instance.new("IntValue")
    Points.Parent = stats
    Points.Name = "Pointz"

    deaths = Instance.new("IntValue")
    deaths.Parent = stats
    deaths.Name = "Deaths"

    Player.CharacterAdded:connect(function()  
       Player.Character.Humanoid.Died:connect(function()
            deaths.Value = deaths.Value + 1
            local deathGui = Instance.new("ScreenGui")
            deathGui.Parent = Player.PlayerGui

            local deathText = Instance.new("TextBox")
            deathText.Parent = deathGui
            deathText.Text = "YOU SUCK, "..Player.Name:upper()
            deathText.FontSize = "Size96"
            deathText.Position = UDim2.new(0.5,0,0.5,0)
            deathText.BackgroundTransparency = 1
            deathText.TextColor3 = Color3.new(255,255,255)
            wait(5)
            deathGui:Destroy() --don't use deprecated methods ; )
        end)
    end)
end)

I'm not the best at explaining, but this should fix your issue :)

3 comments

0

Thank you!

0

No problem! You have no idea how long it took me to figure out localplayer didn't work with server scripts when I first began.

0

Yeah, because all of the tutorial books use them in normal scripts :/

This discussion is preserved as it was. New answers, comments, and votes are closed.