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

Why wont my gamepass give the player 50 speed ?

Asked by 6 years ago

The first part works but the second part with the walk speed doesn't.

local service = game:GetService("GamePassService")

game.Players.PlayerAdded:connect(function(player) 
    if service:PlayerHasPass(player,1008824264) then 
            game.Players.LocalPlayer.leaderstats.Points.Value = game.Players.LocalPlayer.leaderstats.Points.Value + 250
        end
    end)

game.Players.PlayerAdded:connect(function(player) 
    if service:PlayerHasPass(game.Players.LocalPlayer,1008824264) then 
        game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 50
    end
end)
0
How would you assume that the server knows what LocalPlayer is? hiimgoodpack 2009 — 6y

2 answers

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
6 years ago
Edited 6 years ago

I noticed 2 problems with your code, so I'm gonna do my best to explain them.

Problem 1

You can't use LocalPlayer in a server script. This is because LocalPlayer is the client, and the client can't be accessed using a server script. To fix this, use the perimeter you added to the PlayerAdded function.

Problem 2

I would suggest using an elseif statement rather than 2 different PlayerAdded functions. This is because it's completely useless to make 2 PlayerAdded functions because they will both fire every time a new player joins. I also suggest using WaitForChild because if the player hasn't loaded your code will error, returning what didn't load as a nil value.

Your code should look something like this:

local service = game:GetService("GamePassService")

game:GetService("Players").PlayerAdded:Connect(function(player)
    if service:PlayerHasPass(player, 1008824264) then 
        player.leaderstats:WaitForChild("Points").Value = player.leaderstats:WaitForChild("Points").Value + 250 --using the player parimeter rather than localplayer
    elseif service:PlayerHasPass(player, 1008824264) then --elseif statement checking if they own the other pass
        player.Character:WaitForChild("Humanoid").WalkSpeed = 50
    end
end)

If this helped, make sure to accept my answer!

0
no. creeperhunter76 554 — 6y
0
? R_alatch 394 — 6y
0
before you edited, you used LocalPlayer. Also, player.leaderstats will error creeperhunter76 554 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

you don't use LocalPlayer in a regular script, that's only for scripts that run client-side. This means it should only work in studio, because when you're testing in studio, the tester is always the localplayer. the way you want to get the player is by using the first argument in the PlayerAdded event, which returns the player that joined

local service = game:GetService("GamePassService")

game.Players.PlayerAdded:Connect(function(player) 
    if service:PlayerHasPass(player,1008824264) then
       local leaderstats = player:WaitForChild("leaderstats")
           local points = leaderstats:WaitForChild("Points")
       points.Value = points.Value + 250
        end
    end)

game.Players.PlayerAdded:Connect(function(player) 
    if service:PlayerHasPass(player, 1008824264) then 
        player.CharacterAdded:Connect(function(character)-- runs every time the player respawns
            player:WaitForChild("Humanoid").WalkSpeed = 50
        end)
    end
end)

Answer this question