Problem with variables

Hello:

I’m having trouble making a game mechanic. What I want is a rocket to appear (set transparency to 1 and CanCollide to true) after the player decides to rebirth (set money and ore to 0 and set rocket tier to 1, those variables are stores in IntValues). The code to change the variables is in a local script, and the code to make the rocket appear is in a regular script, triggering the script with a remote event.

The issue is that right when the remote event fires, the tier variable sets back to 0, making the rocket not appear at all. Here is the code for the script (it has more functions which I’m not going to show) :

local function onServerEvent(player)
	if player.Name == script.Parent.Parent.Owner.Value then
		if player.stats.RocketTier.Value == 1 then
			script.Parent.Parent.Rocket1:GetChildren().Transparency = 0
			script.Parent.Parent.Rocket1:GetChildren().CanCollide = true
		elseif player.stats.RocketTier.Value == 2 then
			script.Parent.Parent.Rocket2:GetChildren().Transparency = 0
			script.Parent.Parent.Rocket2:GetChildren().CanCollide = true
		end
	end
end

game.ReplicatedStorage.UpdateRocket.OnServerEvent:Connect(onServerEvent)

And here is the local script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function onMouseClick()
	local player = game.Players.LocalPlayer or game.Players:GetPropertyChangedSignal("LocalPlayer"):wait()
	
	if player.stats.RocketTier.Value == 1 then
		if player.leaderstats.Money.Value >= 50000 then
			player.leaderstats.Money.Value = 0
			player.leaderstats.Ore.Value = 0
			player.stats.RocketTier.Value = 2
			wait(0.1)
			ReplicatedStorage.UpdateRocket:FireServer(player)
		end
	end
	if player.stats.RocketTier.Value == 0 then
		if player.leaderstats.Money.Value >= 5000 then
			player.leaderstats.Money.Value = 0
			player.leaderstats.Ore.Value = 0
			player.stats.RocketTier.Value = 1
			wait(0.1)
			ReplicatedStorage.UpdateRocket:FireServer(player)
		end
	end
end

script.Parent.MouseButton1Click:Connect(onMouseClick)

These are files’ locations (ignore the script which parent is ClickDetector):

This is my player while the game’s running (I created 2 folders on purpose):
a a a a

1 Like

You are changing the RocketTier value from a LocalScript and server scripts can’t see changes made by a LocalScript. You need to change the RocketTier value from the server script. Also when you are using RemoteEvent:FireServer(), you don’t need to put the player argument in the (). you only need to put the player argument in the function that is connected to RemoteEvent.OnServerEvent.

1 Like

Wow, it worked. Thank you for your help. I’ll give you a like for that :wink: