Need help to make player be able to walk and jump after respawning when died

Hi, I’m making one of those EA games where you basically have to pay to do stuff like walk, jump, etc. Although I have managed to make it so you need to pay once to walk and again to jump, everytime the player dies and respawn, the prompt appears and they have to pay everytime they respawn which is not what I want. I tried using BoolValues to make it check if they have bought it but it doesn’t work.

Local script inside the StarterGui

local player = game.Players.LocalPlayer
local char = player.Character
local walking = false
local jumping = false

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
		if player.PlayerGui.bools.walking.Value == false then
			if debounce == false then
				debounce = true
				game:GetService("ReplicatedStorage").purchases:FireServer(1)
				wait(5)
				debounce = false
			end
		end
	elseif input.KeyCode == Enum.KeyCode.Space then
		if player.PlayerGui.bools.jumping.Value == false then
			if debounce == false then
				debounce = true
				game:GetService("ReplicatedStorage").purchases:FireServer(2)
				wait(5)
				debounce = false
			end
		end
	end
end)

char:WaitForChild("Humanoid").Died:Connect(function()
	if player.PlayerGui.bools.walking.Value == true then
		print("player died with walking purchased")
		walking = true
	end
	if player.PlayerGui.bools.jumping.Value == true then
		print("player died with jumping purchased")
		jumping = true
	end
end)

player.CharacterAdded:Connect(function()
	print("character loaded, checking")
	if walking == true then
		print("sending remote to server to make player be able to walk")
		game:GetService("ReplicatedStorage").respawn:FireServer(1)
		player.PlayerGui.bools.walking.Value = true
	end
	if jumping == true then
		print("sending remote to server to make player be able to jump")
		game:GetService("ReplicatedStorage").respawn:FireServer(2)
		player.PlayerGui.bools.jumping.Value = true
		end
end)

Server script which handles the purchases, stored inside ServerScriptService

local event = game:GetService('ReplicatedStorage').purchases
local marketplace = game:GetService("MarketplaceService")
local players = game:GetService("Players")

event.OnServerEvent:Connect(function(user, numb)
	if numb == 1 then
		marketplace:PromptProductPurchase(user, 1293370844)
	elseif numb == 2 then
		marketplace:PromptProductPurchase(user, 1293379944)
	end
end)

local function processreceipt(receiptinfo)
	local player = players:GetPlayerByUserId(receiptinfo.PlayerId)
	local char = game.Workspace:FindFirstChild(player.Name)
	if receiptinfo.ProductId == 1293370844 then
		char.Humanoid.WalkSpeed = 16
		player.PlayerGui.bools.walking.Value = true
	elseif receiptinfo.ProductId == 1293379944  then
		char.Humanoid.JumpPower = 50
		player.PlayerGui.bools.jumping.Value = true
		end
end
marketplace.ProcessReceipt = processreceipt

game:GetService("ReplicatedStorage").respawn.OnServerEvent:Connect(function(player, numb)
	local char = player.Character
	if numb == 1 then
		char.Humanoid.WalkSpeed = 16
	elseif numb == 2 then
		char.Humanoid.JumpPower = 50
	end
end)

The bools are stored inside the StarterGui so its both accesed by the client and server side, inside a folder.
image

How can I fix this problem?

Shouldn’t you put the local script into StarterPlayerScripts? I don’t think StarterGui is the right place for this.

Iv put the local script in the StarterPlayerScripts as you said but an error pops up.

image

image (Line 2 and 3 for these 2 locals)

Not really sure how to fix it.

local char = player.Character or CharacterAdded:Wait()
This should fix the error you get

Change your code to this:

--//Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")

--//Variables
local LocalPlayer = Players.LocalPlayer
local PlayerGui = LocalPlayer:WaitForChild("PlayerGui")
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()

--//Controls
local walking = false
local jumping = false
local debounce = false

--//Functions
local function InitializeCharacter(character)
	if walking then
		print("sending remote to server to make player be able to walk")

		ReplicatedStorage.respawn:FireServer(1)
		PlayerGui.bools.walking.Value = true
	end

	if jumping then
		print("sending remote to server to make player be able to jump")

		ReplicatedStorage.respawn:FireServer(2)
		PlayerGui.bools.jumping.Value = true
	end
	
	character:WaitForChild("Humanoid").Died:Connect(function()
		if PlayerGui.bools.walking.Value then
			print("player died with walking purchased")
			walking = true
		end

		if PlayerGui.bools.jumping.Value then
			print("player died with jumping purchased")
			jumping = true
		end
	end)
end

InitializeCharacter(Character)

LocalPlayer.CharacterAdded:Connect(function(character)
	InitializeCharacter(character)
end)

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.D then
		if not PlayerGui.bools.walking.Value then
			if not debounce then
				debounce = true
				
				ReplicatedStorage.purchases:FireServer(1)
				
				task.wait(5)
				debounce = false
			end
		end
	elseif input.KeyCode == Enum.KeyCode.Space then
		if not PlayerGui.bools.jumping.Value then
			if not debounce then
				debounce = true
				
				ReplicatedStorage.purchases:FireServer(2)
				
				task.wait(5)
				debounce = false
			end
		end
	end
end)

Thanks! It works! I just had to add a WaitForChild for line 22 and 29 since it couldn’t instantly find the folder.