Local script not enabling using a different script

alright so i’ve tried to make a camera shake script enable after clicking a button.
but when i try this script

local localscript = game.StarterPlayer.StarterCharacterScripts.CameraShake
script.Parent.MouseButton1Click:Connect(function()
localscript.Enabled = true
end)

but it doesn’t work and nothing shows up on the output. what am i doing wrong???

2 Likes

I would wrap all the code inside of the localscript into a function that’s fired once the script is enabled.

For example:

local function test()
     print("Hi!")
end
test()

By wrapping your code in a function and then calling that same function, you can guarantee that your code will execute as intended when the LocalScript is enabled.

the problem is you’re doing

game.StarterPlayer.StarterCharacterScripts

get the player’s character and get the script that’s in the character instead

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local CameraShake = Character:WaitForChild("CameraShake")

script.Parent.MouseButton1Click:Connect(function()
  CameraShake.Enabled = true
end)
3 Likes

I would recommend against this approach. It would be better to use an event-based system to implement camera shake. You can achieve this with BindableEvents. Fire a BindableEvent when the camera needs to shake. In your Camera Shake script, connect to the BindableEvent’s Event event. You can read more about this object and how to use it here.

I hope this helps!

3 Likes

it worked, tysm! now i can see that i need to learn more stuff

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.