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

Car Regen script using remote events - doesn't work, no errors?

Localscript

local button = script.Parent
local debounce = false
local regenEvent = game.ReplicatedStorage.Regen

local function onHit()
	if debounce then
	end
	debounce = true
	button.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
	regenEvent:FireServer()
	button.BackgroundColor3 = Color3.fromRGB(170, 255, 0)
	debounce = false
end

button.Activated:Connect(onHit)

Script

local regenEvent = game.ReplicatedStorage.Regen

regenEvent.OnServerEvent:Connect(function(player)
	local clone = game.Workspace.CopyPasteChassis:Clone()
	clone.Body.Anchored = false
	local newPos = Vector3.new(0,5,0) + player.Character.PrimaryPart.Position
	clone:SetPrimaryPartCFrame(CFrame.new(newPos)*CFrame.Angles(0,math.rad(player.Character.PrimaryPart.Orientation.Y),0))
end)


The localscript is inside of a button that fires it (or, at least, should fire it) when clicked by the user. The localscript fires the "Regen" event already in replicated storage when the game starts, and the server sided script that resides in workspace should be waiting for that event to be fired and use the CFrame of the character of the player that fired it, which is passed automatically, to adjust the CFrame of a cloned vehicle named CopyPasteChassis that I placed inside of some terrain in the workspace to be close to the player. It also adjust its angle, but that part isn't necessary.

It doesn't work at all. It used to spawn a new car at 0,0,0 but now it just does nothing. It throws no exceptions either so I have no idea how to fix it. Removing the part with CFrame doesn't change anything. It's likely the button isn't firing the event.

2 comments

0

change player.Character.PrimaryPart.Position to player.Character.HumanoidRootPart.Position, primary part is not a valid member of a character but we already know the primary part is the humanoidrootpart

0

Thanks but that doesn't solve the issue of nothing happening when I press the button

2 answers

Issue solved by discord user 2_0: The vehicle needs to be in ReplicatedStorage. If it is cloned there and then reparented to game.Workspace, the scripts work flawlessly.

1 comment

0

No, it does not need to be in replicated storage. That is an old and discouraged practice. Keep your models in ServerStorage, to reduce data transfer and improve performance. Unused models do not need to be replicated to clients.

There are few issues with your script. Let me elaborate (local script first):

local button = script.Parent
local debounce = false
--local regenEvent = game.ReplicatedStorage.Regen
--on the client ALWAYS use WaitForChild(). It may work in studio, but on tablet on slower connections, event may not replicate on time.
local regenEvent = game.ReplicatedStorage:WaitForChild("Regen")

local function onHit()
--	if debounce then
--   end
--that if statement doesn't do anything, because you end it abruptly..
	if not debounce then
		debounce = true
		button.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
		regenEvent:FireServer()
		button.BackgroundColor3 = Color3.fromRGB(170, 255, 0)
		wait(1) -- add some wait before reseting debounce. On a side note, debounce is not really necessary in gui elements, since roblox handles it really well internally.
		debounce = false
	end
end

button.Activated:Connect(onHit)

Now lets look at server script:

local regenEvent = game.ReplicatedStorage.Regen

regenEvent.OnServerEvent:Connect(function(player)

	local clone = game.Workspace.CopyPasteChassis:Clone()
	clone.Body.Anchored = false
	--local newPos = Vector3.new(0,5,0) + player.Character.PrimaryPart.Position
	--as mentioned use HumanoidRootPart for characters. Also make sure character is loaded.
	local character = player.Character or player.CharacterAdded:Wait()
	local newPos = Vector3.new(0,5,0) + player.Character.HumanoidRootPart.Position
	clone:SetPrimaryPartCFrame(CFrame.new(newPos)*CFrame.Angles(0,math.rad(player.Character.PrimaryPart.Orientation.Y),0))
	--is that the end? no you still need to parent your clone
	clone.Parent = workspace --or whatever suit your needs.
end)

Have a nice scripting session.

1 comment

0

I'm guessing you scrolled all the way down without looking at anything on the page.

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